package svc

import (
	"common/commMysql"
	"fmt"
	"gateway/internal/config"
	"gateway/websocket/Client"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"log"
	"time"
)

type ServiceContext struct {
	Config  config.Config
	Manager *Client.ClientManager
	DB      *gorm.DB
}

func NewServiceContext(c config.Config, m *Client.ClientManager) *ServiceContext {
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		c.MYSQL.USER,
		c.MYSQL.PWD,
		c.MYSQL.HOST,
		c.MYSQL.PORT,
		c.MYSQL.Database)
	fg := gorm.Config{
		Logger: logger.New(log.Default(), logger.Config{
			SlowThreshold:             200 * time.Millisecond,
			Colorful:                  true,
			IgnoreRecordNotFoundError: false,
			LogLevel:                  logger.Info,
		}),
	}
	DB := commMysql.InitMysql(dsn, &fg)
	return &ServiceContext{
		Config:  c,
		Manager: m,
		DB:      DB,
	}
}
